<?php
// +----------------------------------------------------------------------
// | 杭州十禾信息科技有限公司
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2015 http://www.shelpe.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: LeePuman <ysyzlsm@163.com>
// +----------------------------------------------------------------------

defined('THINK_PATH') or exit();

class TagLibInvoke extends TagLib
{
    protected static $method = array(
			'get', 'post', 'put', 'delete'
		);
    
    protected $tags   =  array(
            'invoke'=>array('attr'=>'path,params,method'),
            'loadJs'=>array('attr'=>'path', 'close'=>0),
            'loadCss'=>array('attr'=>'path', 'close'=>0)
        );

    public function _invoke($attr,$content) 
    {
        static $_iterateParseCache = array();
        //如果已经解析过，则直接返回变量值
        $cacheIterateId = md5($attr.$content);
        if(isset($_iterateParseCache[$cacheIterateId]))
            return $_iterateParseCache[$cacheIterateId];
        $tag = $this->parseXmlAttr($attr, 'invoke');
        $path = $tag['path'];
        $method = $tag['method'];
        $params = $tag['params'];
        $debug = $tag['debug'];
        $parseStr =  '<?php ';
        $parseStr .= '$path = \'' . $path . '\';';
        $parseStr .= '$method = \'' . $method . '\';';
        $parseStr .= '$params = \'' . $params . '\';';
        if(strlen($path) == 0)
			throw new Exception("invalid path", RetStatus::PARAM_ERR);

        if(!$method)
			throw new Exception("undefined method", RetStatus::PARAM_ERR);

		if(!in_array($method, self::$method))
			throw new Exception("invalid method", RetStatus::PARAM_ERR);
		$url = '';
        switch ($method) {
        	case 'get':
                $parseStr .= '$url = build_url($path, $params);';
        		
        		break;
        }
        $parseStr .= 'require_once LIB_PATH . "Library/TaobaoAPI/Curl.php";';
        $parseStr .= '$curl = new Curl();';
        $parseStr .= '$objResult = $curl->$method($url);';
        $parseStr .= '$objResult = object_to_array($objResult);';
        if($debug){
            $parseStr .= 'var_dump($url);';
            $parseStr .= 'echo "\n";';
            $parseStr .= 'var_dump($objResult);';
        }
        $parseStr .= '?>';
        $parseStr .= $this->tpl->parse($content);
        $_iterateParseCache[$cacheIterateId] = $parseStr;

        if(!empty($parseStr)) {
            return $parseStr;
        }
        return ;
    }

    public function _loadJs($attr,$content)
    {
        static $_iterateParseCache = array();
        //如果已经解析过，则直接返回变量值
        $cacheIterateId = md5($attr.$content);
        if(isset($_iterateParseCache[$cacheIterateId]))
            return $_iterateParseCache[$cacheIterateId];
        $tag = $this->parseXmlAttr($attr, 'invoke');
        $js = explode(',', $tag['path']);
        unset($tag);
        $parseStr = '';
        if($js) {
            $parseStr .= '<?php echo \'';
            foreach ($js as $value) {
                $parseStr .= '<script type="text/javascript" src="/Js/dist/'. $value .'.js?_v='.time().'"></script>';
            }
            $parseStr .= '\';?>';
        }
        return $parseStr;
    }

    public function _loadCss($attr,$content)
    {
        static $_iterateParseCache = array();
        //如果已经解析过，则直接返回变量值
        $cacheIterateId = md5($attr.$content);
        if(isset($_iterateParseCache[$cacheIterateId]))
            return $_iterateParseCache[$cacheIterateId];
        $tag = $this->parseXmlAttr($attr, 'invoke');
        $js = explode(',', $tag['path']);
        unset($tag);
        $parseStr = '';
        if($js) {
            $parseStr .= '<?php echo \'';
            foreach ($js as $value) {
                $parseStr .= '<link rel="stylesheet" href="/Css/'. $value .'.css?_v='.time().'" type="text/css">';
            }
            $parseStr .= '\';?>';
        }
        return $parseStr;
    }
}